package com.itx.gatewayx.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itx.gatewayx.domain.NewsCategory;
import com.itx.gatewayx.mapper.NewsCategoryMapper;
import com.itx.gatewayx.service.NewsCategoryService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 新闻分类服务实现类
 *
 * @author itx
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class NewsCategoryServiceImpl extends ServiceImpl<NewsCategoryMapper, NewsCategory> implements NewsCategoryService {
    
    private final NewsCategoryMapper newsCategoryMapper;
    
    @Override
    public List<NewsCategory> getAllCategories() {
        return newsCategoryMapper.selectAllOrderBySortOrder();
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addCategory(NewsCategory category) {
        // 设置默认值
        category.setCreateTime(new Date());
        category.setUpdateTime(new Date());
        category.setDelFlag(0);
        
        if (category.getSortOrder() == null) {
            category.setSortOrder(0);
        }
        
        return this.save(category);
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateCategory(NewsCategory category) {
        // 设置更新时间
        category.setUpdateTime(new Date());
        
        // 不允许更新删除标志
        category.setDelFlag(null);
        
        return this.updateById(category);
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteCategory(String categoryId) {
        // 逻辑删除
        NewsCategory category = new NewsCategory();
        category.setId(categoryId);
        category.setDelFlag(1);
        category.setUpdateTime(new Date());
        
        return this.updateById(category);
    }
    
    @Override
    public NewsCategory getCategoryById(String categoryId) {
        return this.getById(categoryId);
    }
} 